<template>
  <div
    class="modal fade"
    tabindex="-1"
    role="dialog"
    aria-hidden="true"
    :id="id"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">新建支付方式</h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 名称 </label>
              <div class="col-9">
                <input type="text" class="form-control" v-model.trim="name" />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end">
                拼音（用于排序）
              </label>
              <div class="col-9">
                <input type="text" class="form-control" v-model.trim="pinyin" />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 开户行 </label>
              <div class="col-9">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="bankName"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 开户名 </label>
              <div class="col-9">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="accountName"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 开户账号 </label>
              <div class="col-9">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="accountNo"
                />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 备注 </label>
              <div class="col-9">
                <input type="text" class="form-control" v-model.trim="remark" />
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 状态 </label>
              <div class="col-9">
                <div class="form-check form-check-inline">
                  <input
                    class="form-check-input"
                    type="radio"
                    value="0"
                    v-model.number="status"
                  />
                  <label class="form-check-label text-danger">停用</label>
                </div>
                <div class="form-check form-check-inline">
                  <input
                    class="form-check-input"
                    type="radio"
                    value="1"
                    v-model.number="status"
                  />
                  <label class="form-check-label text-success">启用</label>
                </div>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import $ from "jquery";

export default {
  props: {
    nullable: {
      type: Boolean,
      default: true,
    },
    id: {
      type: String,
      default: "supplierModal",
    },
    paymentMethods: {
      type: Array,
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      pmId: 0,
      name: "",
      pinyin: "",
      bankName: "",
      accountName: "",
      accountNo: "",
      remark: "",
      status: 1,

      myModal: null,
    };
  },
  methods: {
    ok: function () {
      this.hideModal();

      this.resolve({
        id: this.pmId,
        name: this.name,
        pinyin: this.pinyin,
        bankName: this.bankName,
        accountName: this.accountName,
        accountNo: this.accountNo,
        remark: this.remark,
        status: this.status,
      });
    },
    modal: function (o) {
      this.pmId = o.id;
      this.name = o.name;
      this.pinyin = o.pinyin;
      this.bankName = o.bankName;
      this.accountName = o.accountName;
      this.accountNo = o.accountNo;
      this.remark = o.remark;
      this.status = o.status;

      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
